import pygame
from pytmx import load_pygame
from Action.Boundary import boundary
from Action.Create_sprite import create_sprite

from Game import SCREEN_WIDTH, SCREEN_HEIGHT

def step10_refactor_actor():
    """
    step10: 重构角色
    :return:
    """
    pygame.init()
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode((1280, 720), 0, 32)
    background = pygame.image.load("../Resources/tmx/map2-1.png")
    tiled_map_data = load_pygame("../Resources/tmx/map2.tmx")
    obstacle_group = pygame.sprite.Group()
    japanese_group = pygame.sprite.Group()
    prizes_group = pygame.sprite.Group()
    yu_er, captain, vice_captain = create_sprite(tiled_map_data, obstacle_group, japanese_group, prizes_group)

    while True:
        down_flag = False
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN:
                down_flag = True
        key_list = pygame.key.get_pressed()
        view_x, view_y = boundary(yu_er.pos_x, yu_er.pos_y)
        sub_view = background.subsurface((view_x, view_y, SCREEN_WIDTH, SCREEN_HEIGHT))
        screen.blit(sub_view, (0, 0))
        yu_er.draw(screen, view_x, view_y)
        yu_er.run(down_flag, key_list)
        captain.draw(screen, view_x, view_y)
        vice_captain.draw(screen, view_x, view_y)
        for japanese in japanese_group:
            japanese.draw(screen, view_x, view_y)
        for prize in prizes_group:
            prize.draw(screen, view_x, view_y)
        for japanese in japanese_group:
            japanese.run(down_flag, key_list)
        clock.tick(20)
        pygame.display.flip()


if __name__ == '__main__':
    step10_refactor_actor()
